const superagent = require('superagent'); // superAgent 是以 utf8 去取数据
require('superagent-charset')(superagent); // 插件，可以指定获取数据格式
import { getHotNewsList } from '../util/common';
import { Injectable } from '@nestjs/common';
import { spider1 } from '../util/spider';
import { getStorage } from '../util/storage';

@Injectable()
export class FinanceService {
    async getEast() {
        const cach = getStorage('east');
        if (cach) {
            return cach;
        }

        const html = await spider1('http://www.eastmoney.com/', false);
        return getHotNewsList(html, 'east', '.hsgs-news-list .nlist a');
    }

    async getTong() {
        const cach = getStorage('tong');
        if (cach) {
            return cach;
        }
        const html = await spider1('http://www.10jqka.com.cn/', true);
        return getHotNewsList(html, 'tong', '.toutiao .tt_word a, .cjyw li a, .secpage .cpbd .newhe li a');
    }

    async getSina() {
        const cach = getStorage('sina');
        if (cach) {
            return cach;
        }
        const html = await spider1('http://finance.sina.com.cn/', false);
        return getHotNewsList(html, 'sina', '.m-p1-r-blk1 .m-p1-rb1-news h3 a, .m-p1r-blk2 li a:last-child');
    }
}
